/**
 * Created by Administrator on 2018/10/28.
 */
(function () {
    (function (doc, win) {
        var docEl = doc.documentElement;
        var resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
        var recalc = function () {
            var clientWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
            var fontsize = 20 * (clientWidth / 1080);
            docEl.style.fontSize = ((fontsize > 20) ? 20 : fontsize) + 'px';
        };
        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
    })(window.document, window);
})();

$(function () {

})

function _alert(msg) {
    layer.msg(msg);
}

function ajax(opts) {
    var async = true;
    if (opts.async == false) {
        async = false;
    }
    var ajaxOpts = {
        type: 'POST',
        url: 'http://server.yemanputao.com/' + opts.url,
        timeout: 30000,
        async: async,
        data: JSON.stringify(opts.data || {}),
        dataType: 'json',
        contentType: 'application/json',
        headers: {
            platform: '123',//平台
            channel: '123',//渠道
            deviceId: '123',//设备ID
            model: '312',//设备类型（iphone,ipod之类）
            appVersion: '222',//app版本号
            packageName: '123',//包名
            token: getToken() || null,
            userId: getUserId() || null,

        },
        success: function (d) {
            if (typeof d.message.code != 'undefined') { // 确认返回了编码
                switch (d.message.code) {
                    case '200':
                        if (opts.success) opts.success(d.data);
                        break;
                    case '-10001':
                        _alert('请先登录');
                        break;
                    default:
                        if (opts.fail) {
                            opts.fail(d)
                        } else {
                            _alert(d.message.msg || '请求错误，请重试');
                        }
                        break;
                }
            } else {
                if (opts.fail) {
                    opts.fail(d.data, d.message);
                } else {
                    _alert(d.message || '请求错误，请重试');
                }
            }
        },
        error: function (d) {
            var responseText = JSON.parse(d.responseText);
            if (opts.error) {
                opts.error(responseText);
            } else {
                _alert(responseText.message || '请求失败');
            }
        }
    };
    $.ajax(ajaxOpts);

}

function getOrderId(){
    return getCookie('_orderId');
}

function setOrderId(data){
    setCookie('_orderId', data);
}

function setUserInfo(opts) {
    setToken(opts.token);
    setUserId(opts.userId);
}

function removeUserInfo() {
    removeToken();
    removeUserId();
}

function getToken() {
   return getCookie('_loginToken');
}

function setToken(data) {
    setCookie('_loginToken', data);
}

function removeToken(data) {
    removeCookie('_loginToken')
}

function getUserId() {
    return getCookie('_userId');
}

function setUserId(data) {
    setCookie('_userId', data);
}

function removeUserId() {
    removeCookie('_userId')
}


// 本地存储- 存储  并设置过期分钟
function setCookie(key, data, minutes) {
    var d = {};
    d.data = data;
    if (!!minutes) {
        d.beginTime = (new Date()).getTime();
        d.effectiveMin = minutes;
    }
    d = JSON.stringify(d);
    window.localStorage.setItem(key, d);
}


// 本地存储- 获取
function getCookie(key) {
    if (!!window.localStorage.getItem(key)) {
        var d = $.parseJSON(window.localStorage.getItem(key));
        var endTime = (new Date()).getTime();
        if (!!d.beginTime && !!d.effectiveMin) {
            if (getDateInterval(d.beginTime, endTime, 'minutes') > parseInt(d.effectiveMin)) {
                window.localStorage.removeItem(key);
                return null;
            } else {
                if (typeof d.data != 'undefined') {
                    return d.data;
                } else {
                    window.localStorage.removeItem(key);
                    return null;
                }
            }
        } else {
            if (!!d.data) {
                return d.data;
            }
        }
    }
    return null;
}

// 本地存储- 删除
function removeCookie(key) {
    if (!!window.localStorage.getItem(key)) {
        window.localStorage.removeItem(key);
    }
}


// 根据类型获取日期间隔
// beginTime 起始时间  endTime 结束时间
// type  日  时 分 秒 类型
function getDateInterval(beginTime, endTime, type) {
    if (typeof type == 'undefined') {
        return;
    }
    beginTime = parseInt(beginTime);
    endTime = parseInt(endTime);
    switch (type) {
        case 'day':
            return Math.abs(endTime - beginTime) / 1000 / 60 / 60 / 24;
        case 'hour':
            return Math.abs(endTime - beginTime) / 1000 / 60 / 60;
        case 'minutes':
            return Math.abs(endTime - beginTime) / 1000 / 60;
        case 'seconds':
            return Math.abs(endTime - beginTime) / 1000;
    }

}